package com.fyh.controller;

import com.fyh.constant.MessageConstant;
import com.fyh.entity.common.Result;
import com.fyh.entity.community.*;
import com.fyh.service.CommunityService;
import com.fyh.util.OssUtil;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/community")
public class CommunityController {

    @Autowired
    CommunityService communityService;

    /**
     * 获取帖子类型列表
     * @return
     */
    @GetMapping("/getCategoryList")
    public Result<List<Category>> getCategoryList(){
        log.info("获取帖子类型列表");
        List<Category> categoryList = communityService.getCategoryList();
        return Result.success(categoryList);
    }

    /**
     * 上传附件
     * @param file
     * @return
     */
    @PutMapping("/uploadAttachment")
    public ResponseEntity<Result<String>> uploadAttachment(@RequestParam("file") MultipartFile file){
        log.info("上传附件");
        String attachmentPath = communityService.uploadAttachment(file);
        if(attachmentPath == null){
            return ResponseEntity
                    .badRequest()
                    .body(Result.error(MessageConstant.UPLOAD_FILE_ERROR));
        }
        return ResponseEntity
                .ok()
                .body(Result.success(attachmentPath));
    }

    /**
     * 发布帖子
     * @param postDTO
     * @param userId
     * @return
     */
    @PostMapping("/addPost")
    public Result addPost(@RequestBody PostDTO postDTO, @RequestHeader("userId") Long userId){
        log.info("用户{}发布了新帖子{}", userId, postDTO.getTitle());
        PostVO postVO = new PostVO();
        BeanUtils.copyProperties(postDTO, postVO);
        postVO.setUserId(userId);
        communityService.addPost(postVO);
        return Result.success();
    }

    /**
     * 分页获取帖子列表
     * @param q
     * @param categoryId
     * @param sort
     * @param offset
     * @param userId
     * @return
     */
    @GetMapping("/getPostList")
    public Result<List<PostVO>> getPostList(@RequestParam("q") String q, @RequestParam(value = "categoryId", required = false) Long categoryId, @RequestParam("sort") String sort, @RequestParam("offset") Integer offset, @RequestHeader("userId") Long userId){
        log.info("获取帖子列表");
        PostPageDTO postPageDTO = new PostPageDTO();
        postPageDTO.setUserId(userId);
        postPageDTO.setQ(q);
        postPageDTO.setSort(sort);
        postPageDTO.setCategoryId(categoryId);
        postPageDTO.setOffset(offset);
        List<PostVO> postVOList = communityService.getPostList(postPageDTO);
        return Result.success(postVOList);
    }

    /**
     * 交互（点赞，点踩，取消）
     * @param reactionDTO
     * @param userId
     * @return
     */
    @PostMapping("/reaction")
    public Result reaction(@RequestBody ReactionDTO reactionDTO, @RequestHeader("userId") Long userId){
        log.info("用户{}执行交互：{}", userId, reactionDTO);

        reactionDTO.setUserId(userId);

        communityService.reaction(reactionDTO);
        return Result.success();
    }

    /**
     * （取消）收藏帖子
     * @param reactionDTO
     * @param userId
     * @return
     */
    @PostMapping("/favorite")
    public Result favorite(@RequestBody ReactionDTO reactionDTO, @RequestHeader("userId") Long userId){
        reactionDTO.setUserId(userId);
        communityService.favorite(reactionDTO);
        return Result.success();
    }

    /**
     * 获取帖子详情
     * @param postId
     * @return
     */
    @GetMapping("/postDetail")
    public Result<PostVO> getPostDetail(@RequestParam("postId") Long postId, @RequestHeader("userId") Long userId){
        log.info("获取帖子{}详情", postId);
        PostVO postVO = communityService.getPostDetail(postId, userId);
        return Result.success(postVO);
    }

    /**
     * 获取帖子一级回复列表（分页查询）
     * @param replyPageDTO
     * @param userId
     * @return
     */
    @PostMapping("/postDetail/replyList")
    public Result<List<ReplyVO>> getReplyList(@RequestBody ReplyPageDTO replyPageDTO, @RequestHeader("userId") Long userId){
        log.info("获取帖子的回复列表");
        replyPageDTO.setUserId(userId);
        List<ReplyVO> replyVOList = communityService.getReplyListByPostId(replyPageDTO);
        return Result.success(replyVOList);
    }

    /**
     * 获取帖子二级回复列表（分页查询）
     * @param replyPageDTO
     * @param userId
     * @return
     */
    @PostMapping("/postDetail/replyReplyList")
    public Result<List<ReplyVO>> getReplyReplyList(@RequestBody ReplyPageDTO replyPageDTO, @RequestHeader("userId") Long userId){
        log.info("获取帖子的二级回复列表");
        replyPageDTO.setUserId(userId);
        List<ReplyVO> replyReplyVOList = communityService.getReplyReplyList(replyPageDTO);
        return Result.success(replyReplyVOList);
    }

    /**
     * 发送回复
     * @param replyDTO
     * @param userId
     * @return
     */
    @PostMapping("/sendReply")
    public Result sendReply(@RequestBody ReplyDTO replyDTO, @RequestHeader("userId") Long userId){
        log.info("发送回复{}", replyDTO);
        replyDTO.setUserId(userId);
        communityService.addReply(replyDTO);
        return Result.success();
    }

}
